/**
Engine.h
Title: Binge Engine Header File
Descr: Engine Class

Copyright (c) 2010, Brant Ryan Anderson
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the <ORGANIZATION> nor the names of its contributors
      may be used to endorse or promote products derived from this software
      without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**/

#ifndef BINGE_ENGINE_H
#define BINGE_ENGINE_H

#include <unistd.h>
#include <ctime>
#include <iostream>
#include <string>
#include <sstream>
#include <list>
#include <vector>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "../../binge/BaseGame.h"
#include "../../binge/core/Audio.h"
#include "../../binge/core/Timer.h"
#include "../../binge/utilities/Vector3.h"
#include "../../binge/system/GameObjects.h"
#include "../../binge/system/AnimationData.h"
#include "../../binge/system/MovementData.h"
#include "../../binge/system/PositionData.h"
#include "../../binge/system/RenderData.h"
#include "../../binge/system/CollisionData.h"
#include "../../binge/system/MapData.h"
#include "../../binge/system/GravityData.h"
#include "../../binge/system/Quadtree.h"

#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_REVISION 999
#define VERSION_RELEASE 59

namespace binge
{

struct TileCollisionCorners
{
    bool upLeft;
    bool downLeft;
    bool upRight;
    bool downRight;
    bool upCenter;
    bool downCenter;
    bool centerLeft;
    bool centerRight;
    std::vector<int> upLeftIds;
    std::vector<int> downLeftIds;
    std::vector<int> upRightIds;
    std::vector<int> downRightIds;
    std::vector<int> upCenterIds;
    std::vector<int> downCenterIds;
    std::vector<int> centerLeftIds;
    std::vector<int> centerRightIds;
};

class Engine
{
public:
    Engine(void);
    virtual ~Engine(void);

    bool gameover;
    sf::RenderWindow screen;
    Audio audio;
    GameObjects gameObjects;
    AnimationData animation;
    MovementData movement;
    PositionData position;
    RenderData render;
    CollisionData collision;
    MapData map;
    GravityData gravity;

    bool Init(void);

    void Close(	void (BaseGame::*game_end)(void),
                            BaseGame* basegame);
    void Update(void (BaseGame::*game_update)(void),
                            void (BaseGame::*game_render)(void),
                            BaseGame* basegame);

    void Message(std::string message, std::string title);
    void FatalError(std::string message, std::string title);
    void Shutdown(void);
    bool RenderStart(void);
    bool RenderStop(void);
    int Release(void);

    //accessor/mutator functions expose the private variables
    bool IsMouseCursorVisible(void) { return mouseVisible; }
    void SetMouseCursorVisible(bool visible) { mouseVisible = visible; screen.ShowMouseCursor(mouseVisible); }
    GameState GetGameMode(void) { return gameState; }
    void SetGameMode(enum GameState mode) { gameState = mode; }
    std::string GetGameTitle(void) { return gameTitle; }
    void SetGameTitle(std::string title) { gameTitle = title; }

    int GetVersionMajor(void) { return versionMajor; }
    int GetVersionMinor(void) { return versionMinor; }
    int GetVersionRevision(void) { return versionRevision; }
    int GetVersionRelease(void) { return versionRelease; }
    std::string GetVersionText(void);

    long GetFrameRate_core(void) { return frameRateCore; }
    long GetFrameRate_real(void) { return frameRateReal; }

    int GetScreenWidth(void) { return screenWidth; }
    void SetScreenWidth(int value) { screenWidth = value; }
    int GetScreenHeight(void) { return screenHeight; }
    void SetScreenHeight(int value) { screenHeight = value; }
    int GetColorDepth(void) { return colorDepth; }
    void SetColorDepth(int value) { colorDepth = value; }
    bool GetFullscreen(void) { return fullscreen; }
    void SetFullscreen(bool value) { fullscreen = value; }

    bool GetMaximizeProcessor(void) { return maximizeProcessor; }
    void SetMaximizeProcessor(bool value) { maximizeProcessor = value; }
    int GetEngineFrameRateMS(void) { return engineFrameRateMS; }
    void SetEngineFrameRateMS(int ms) { engineFrameRateMS = ms; }

    std::vector<int> CollidesWithPoint(float x, float y); // Returns vector of objId's
    int CheckCollisionSide(sf::Rect<float> r1, sf::Rect<float> r2); // Returns the sides collided on.

private:
    int versionMajor, versionMinor;
    int versionRevision, versionRelease;
    std::string gameTitle;
    bool fullscreen;
    int screenWidth;
    int screenHeight;
    int colorDepth;
    bool mouseVisible;
    enum GameState gameState;
    bool maximizeProcessor;
    long frameCountCore;
    long frameRateCore;
    long frameCountReal;
    long frameRateReal;
    int engineFrameRateMS;


    // Engine Timers:
    Timer timedUpdate;
    Timer coreTimer;
    Timer realTimer;
    Timer collisionTimer;


    bool InitSFML(void);

    // Engine Subsystems
    void AnimationSystem(void);

    void MovementSystem(    void (BaseGame::*game_gob_moved)(int id),
                            BaseGame* basegame);

    GLuint prevTexture;
    void RenderSystem(	void (BaseGame::*game_gob_rendered)(int id),
                            BaseGame* basegame);

    Quadtree* quadtree;
    void TileCollisions( int objId, float xvel, float yvel,
                        void (BaseGame::*game_tile_collision)(int objId, int tileId, int side),
                        BaseGame* basegame);
    TileCollisionCorners tcCorners;
    void GetMyCorners(int objId, float xvel, float yvel, bool axis_is_x);

    bool Collision(int id1, int id2);
    bool CollisionBR(int id1, int id2);
    bool CollisionD(int id1, int id2);
    void CollisionSystem(   void (BaseGame::*game_gob_collision)(int id1, int id2, int sides),
                            BaseGame* basegame);

    void GOBCleanup(    void (BaseGame::*game_gob_removed)(int id),
                        BaseGame* basegame);
};

} //namespace

#endif
